package com.helloniu.wb.tools {

import com.helloniu.wb.CursorManager2;

import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import com.helloniu.fl.util.Tool;

public class RectangleResizeBox extends ResizeBox {
    public function RectangleResizeBox() {
        super();
    }

    override protected function setupListeners():void{
        super.setupListeners();

        addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
        addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
    }

	private function mouseOverHandler(event:MouseEvent):void {
    	var currPoint:Point = new Point(event.localX, event.localY);

        for(var i:int = 0; i < rectArr.length; i++) {
            if((rectArr[i] as Rectangle).containsPoint(currPoint)) {
            	showCursor(i);
                return;
            }
        }

        // @temp tonyy
        //drawRect(x, y, w, h);
        //var boxRect:Rectangle = new Rectangle(x + 3, y + 3, w - 6, h - 6);
        //if(boxRect.containsPoint(currPoint)) {
        //	Tool.traceT("______________________ssssssssss:");
        //}
    }

    private function showCursor(idx:int):void {
    	if(idx == 0 || idx == 7) {
        	CursorManager2.setCursor(CursorManager2.RESIZE_07_CURSOR)
        }
        else if(idx == 1 || idx == 6) {
        	CursorManager2.setCursor(CursorManager2.RESIZE_16_CURSOR)
        }
        else if(idx == 2 || idx == 5) {
        	CursorManager2.setCursor(CursorManager2.RESIZE_25_CURSOR)
        }
        else if(idx == 3 || idx == 4) {
        	CursorManager2.setCursor(CursorManager2.RESIZE_34_CURSOR)
        }
    }

	private function mouseOutHandler(event:MouseEvent):void {
    	CursorManager2.removeCursor();
    }

    public function setStartPoint(sp:Point):void {
        startP = sp;
    }

    public function setWidth(width0:Number):void {
        w = width0;
    }

    public function setHeight(height0:Number):void {
        h = height0;
    }

    public function getResizeType():Number {
        return resizeType;
    }

    override protected function updateDisplayList(uw:Number, uh:Number):void {
        super.updateDisplayList(uw, uh);

        if(this.stage == null || startP == null) {
            return;
        }

        var x:Number = startP.x;
        var y:Number = startP.y;

        with(graphics) {
            clear();
            lineStyle(1, getStyle("themeColor"), 0.5);
            beginFill(getStyle("themeColor"), 0.1);
            //Tool.traceT("----------themeColor: " + getStyle("themeColor"));
            rectArr = new Array();

            // 0
            moveTo(x - offset, y - offset);
            rectArr.push(new Rectangle(x - offset, y - offset, areaWidth, areaHeight));
            drawRect(x - offset, y - offset, areaWidth, areaHeight);

            // 1
            moveTo(x - offset + w/2, y - offset);
            rectArr.push(new Rectangle(x - offset + w/2, y - offset, areaWidth, areaHeight));
            drawRect(x - offset + w/2, y - offset, areaWidth, areaHeight);
            // 2
            moveTo(x - offset + w, y - offset);
            rectArr.push(new Rectangle(x - offset + w, y - offset, areaWidth, areaHeight));
            drawRect(x - offset + w, y - offset, areaWidth, areaHeight);
            // 3
            moveTo(x - offset, y - offset + h/2);
            rectArr.push(new Rectangle(x - offset, y - offset + h/2, areaWidth, areaHeight));
            drawRect(x - offset, y - offset + h/2, areaWidth, areaHeight);
            // 4
            moveTo(x - offset + w, y - offset + h/2);
            rectArr.push(new Rectangle(x - offset + w, y - offset + h/2, areaWidth, areaHeight));
            drawRect(x - offset + w, y - offset + h/2, areaWidth, areaHeight);
            // 5
            moveTo(x - offset, y - offset + h);
            rectArr.push(new Rectangle(x - offset, y - offset + h, areaWidth, areaHeight));
            drawRect(x - offset, y - offset + h, areaWidth, areaHeight);
            // 6
            moveTo(x - offset + w/2, y - offset + h);
            rectArr.push(new Rectangle(x - offset + w/2, y - offset + h, areaWidth, areaHeight));
            drawRect(x - offset + w/2, y - offset + h, areaWidth, areaHeight);
            // 7
            moveTo(x - offset + w, y - offset + h);
            rectArr.push(new Rectangle(x - offset + w, y - offset + h, areaWidth, areaHeight));
            drawRect(x - offset + w, y - offset + h, areaWidth, areaHeight);
            endFill();

            lineStyle(1, getStyle("themeColor"), 0.5);
            moveTo(x, y);
            drawRect(x, y, w, h);
        }
    }

    private var w:Number;
    private var h:Number;
}
}